﻿using Service.Auth.Domain.Events;
using Service.Repository;
using SqlSugar;

namespace Service.Auth.Domain.AggregatesModel
{

    /// <summary>
    /// 角色聚合
    /// </summary>
    [SugarTable("tb_role")]
    public class Role : Entity
    {

        public Role() { }

        public Role(long id, string roleNum, string roleName, string remarks, int level, long systemId)
        {
            Id = id;
            RoleNum = roleNum;
            RoleName = roleName;
            Remarks = remarks;
            Level = level;
            SystemId = systemId;
            Default = false;
        }


        /// <summary>
        /// 角色名称
        /// </summary>
        [SugarColumn(ColumnName = "id", IsPrimaryKey = true)]
        public long Id { get; private set; }

        /// <summary>
        /// 角色编号
        /// </summary>
        [SugarColumn(ColumnName = "role_num")]
        public string RoleNum { get; private set; }

        public void SetRoleNum(string value)
        {
            RoleNum = value;
        }

        /// <summary>
        /// 角色名称
        /// </summary>
        [SugarColumn(ColumnName = "role_name")]
        public string RoleName { get; private set; }

        public void SetRoleName(string value)
        {
            RoleName = value;
        }


        /// <summary>
        /// 角色备注
        /// </summary>
        [SugarColumn(ColumnName = "remarks")]
        public string Remarks { get; private set; }

        public void SetRemarks(string value)
        {
            Remarks = value;
        }

        /// <summary>
        /// 角色级别
        /// </summary>
        [SugarColumn(ColumnName = "level")]
        public int Level { get; private set; }

        public void SetLevel(int value)
        {
            Level = value;
        }



        /// <summary>
        /// 角色级别
        /// </summary>
        [SugarColumn(ColumnName = "system_id")]
        public long SystemId { get; private set; }


        [Navigate(NavigateType.OneToOne, nameof(SystemId))]
        public TSystem System { get; set; }

        /// <summary>
        /// 是否默认
        /// </summary>
        [SugarColumn(ColumnName = "is_default")]
        public bool Default { get; set; }


        /// <summary>
        /// 设置角色菜单
        /// </summary>
        /// <param name="roles"></param>
        public void SetPermission(List<long> prmissionIds)
        {
            AddDomainEvent(new RoleAddPermissionsDomainEvent(Id, prmissionIds));
        }

        /// <summary>
        /// 设置角色菜单
        /// </summary>
        /// <param name="roles"></param>
        public void Update(string roleNum, string roleName, string remarks)
        {
            if ((RoleNum + RoleName).GetHashCode() != (roleNum + roleName).GetHashCode())
            {
                AddDomainEvent(new RoleChangeInfoDomainEvent(Id, roleNum, roleName));
            }
            RoleNum = roleNum;
            RoleName = roleName;
            Remarks = remarks;


        }


    }
}
